﻿using System;
using System.ComponentModel;
using System.Web.Mvc;
using viBook.Controllers;
using viBook.Models;

namespace viBook.ModelBinders
{
    public class BookBinders : DefaultModelBinder
    {
        protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
        {
            if (propertyDescriptor.Name == "UploadedDate")
            {
                propertyDescriptor.SetValue(bindingContext.Model, DateTime.Now);
                return;
            }
            if (propertyDescriptor.Name == "UploadedBy")
            {
                propertyDescriptor.SetValue(bindingContext.Model, ((BaseController)controllerContext.Controller).ExtendedIdentity.Id);
                return;
            } 
            if (propertyDescriptor.Name == "Uploader")
            {
                propertyDescriptor.SetValue(bindingContext.Model, ((BaseController)controllerContext.Controller).ExtendedIdentity.Name);
                return;
            }
            if (propertyDescriptor.Name == "CoverUrl")
            {
                propertyDescriptor.SetValue(bindingContext.Model, controllerContext.HttpContext.Request["filesUploaded"]);
                return;
            }
            base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
        }
    }
}